﻿
//Author: 张亚涛
//createtime:2015-12-17 22:45:30
//Description:

using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using NS.DAL.Systems;
using Model;
using DAL;

namespace DAL
{
    /// <summary>
    /// 该类提供了一系列操作 "MemberCar" 表的方法;
    /// </summary>
    public class MemberCarBase
    {
        const string SQL_Select = "select [ID],[OpenID],[CarTypeID],[CarNumber],[Color] from [MemberCar] ";
        #region .对本表的维护.
        #region select data
        /// <summary>
        /// 返回MemberCar表的所有数据 
        /// </summary>
        /// <param name="OpenID">微信ID</param>
        /// <returns></returns>        
        public static List<MemberCar> GetAllMemberCar(string OpenID)
        {
            List<MemberCar> memberCarList = new List<MemberCar>();
            SqlDataReader reader = null;

            string sql = SQL_Select + "where [OpenID] = @OpenID";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@OpenID",OpenID)
            };
            reader = SQLHelper.ExecuteDataReader(sql, paras);
            while (reader.Read())
            {
                MemberCar memberCar = new MemberCar(reader);
                memberCarList.Add(memberCar);
            }
            reader.Close();
            return memberCarList;
        }
        /// <summary>
        /// 根据MemberCar表的ID字段返回数据 
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns></returns>       
        public static MemberCar GetMemberCarById(long id)
        {
            MemberCar memberCar = null;
            SqlDataReader reader = null;

            string sql = SQL_Select + "where [ID] = @Id";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",id)
            };
            reader = SQLHelper.ExecuteDataReader(sql, paras);
            if (reader.Read())
            {
                memberCar = new MemberCar(reader);
            }
            reader.Close();
            return memberCar;
        }
        /// <summary>
        /// 获得车辆颜色
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns></returns>       
        public static string GetColor(long id)
        {
            string sql = "SELECT Color FROM [MemberCar] WHERE [ID] = @Id";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",id)
            };
            return SQLHelper.ExecuteScalar(sql, paras);
        }
        /// <summary>
        /// 根据会员ID字段返回会员车辆信息
        /// </summary>
        /// <param name="OpenID">微信ID</param>
        /// <returns></returns>       
        public static MemberCar GetMemberCarByOpenID(string OpenID)
        {
            MemberCar memberCar = null;
            SqlDataReader reader = null;

            string sql = SQL_Select + "where [OpenID] = @OpenID";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@OpenID",OpenID)
            };
            reader = SQLHelper.ExecuteDataReader(sql, paras);
            if (reader.Read())
            {
                memberCar = new MemberCar(reader);
            }
            reader.Close();
            return memberCar;
        }
        #endregion
        #region delete data
        /// <summary>
        /// 根据MemberCar表的ID字段删除数据 
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>        
        public static int DeleteMemberCarById(long id)
        {
            string sql = "delete from [MemberCar] where [ID] = @Id";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",id)
            };
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region update data
        /// <summary>
        /// prepare parameters 
        /// </summary>
        public static SqlParameter[] PrepareCommandParameters(MemberCar memberCar)
        {
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",memberCar.Id),
            new SqlParameter("@OpenID",memberCar.OpenID),
            new SqlParameter("@CarTypeID",memberCar.CarTypeID),
            new SqlParameter("@CarNumber",memberCar.CarNumber),
            new SqlParameter("@Color",memberCar.Color)
            };
            return paras;
        }
        /// <summary>
        /// 根据MemberCar表的ID字段更新数据 
        /// </summary> 
        /// <param name="MemberCar">memberCar</param>
        /// <returns></returns>       
        public static int UpdateMemberCarById(MemberCar memberCar)
        {
            string sql = "update [MemberCar] set [OpenID] = @OpenID,[CarTypeID] = @CarTypeID,[CarNumber] = @CarNumber,[Color] = @Color where [ID] = @Id";
            SqlParameter[] paras = PrepareCommandParameters(memberCar);
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region insert data
        /// <summary>
        /// 向MemberCar表插入一条数据，返回自增列数值，插入不成功则返回-1
        /// </summary>
        /// <param name="memberCar">MemberCar</param>       
        /// <returns></returns>        
        public static int AddMemberCar(MemberCar memberCar)
        {
            string sql = "insert into [MemberCar]([OpenID],[CarTypeID],[CarNumber],[Color])values(@OpenID,@CarTypeID,@CarNumber,@Color)    select @@identity";
            SqlParameter[] paras = PrepareCommandParameters(memberCar);
            int id = -1;

            string _ID = SQLHelper.ExecuteScalar(sql, paras).ToString();
            int.TryParse(_ID, out id);

            return id;
        }
        #endregion
        #endregion

        #region .SQL语句汇总.
        /// <summary>
        /// 返回MemberCar表的所有数据 （SQL语句）
        /// </summary>
        /// <returns></returns>        
        public static string GetAllMemberCar_SQL()
        {
            return SQL_Select;
        }
        #endregion
    }
}